<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE xsl:stylesheet [
<!ENTITY css SYSTEM "../phaidros.css">
]>
<xsl:stylesheet version="1.0" xmlns="http://www.w3.org/1999/xhtml" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" encoding="utf-8" indent="yes" doctype-public="-//W3C//DTD XHTML 1.0 Transitional//EN" 
doctype-system="http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"/>

<xsl:include href="topbuttons.xsl"/>

<!-- Stylesheet to edit a twist down list of versions -->

<!-- main HTML template -->
<xsl:template match="/">
<xsl:variable name="file" select="//response/@file"/>
<xsl:variable name="addGroupName" select="*/strings/string[@name='AddGroupButton']/@value"/>
<xsl:variable name="addVersionName" select="*/strings/string[@name='AddVersionButton']/@value"/>
<xsl:variable name="editGroupName" select="*/strings/string[@name='EditGroupButton']/@value"/>
<xsl:variable name="editVersionName" select="*/strings/string[@name='EditVersionButton']/@value"/>
<xsl:variable name="deleteButtonName" select="*/strings/string[@name='DeleteButton']/@value"/>
<html>
<head>
<style type="text/css">
&css;
</style>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />

<script type="text/javascript">
function deleteVersion( version )
{
	var answer = confirm("This will delete the version and all its contents");
	if ( answer )
	{
		var com = document.getElementById("Command");
		var versionField = document.getElementById("Version1");
		versionField.value = version;
		com.value = "DeleteVersion";
		document.submission.action="/Alpha/EditVersions";
		document.submission.submit();
	}
}
function deleteGroup( group )
{
	var answer = confirm("Do you really want to delete this group, its versions and their contents?");
	if ( answer )
	{
		var com = document.getElementById("Command");
		var groupField = document.getElementById("GroupId");
		com.value = "DeleteGroup";
		groupField.value = group;
		document.submission.action="/Alpha/EditVersions";
		document.submission.submit();
	}
}
function revert()
{
	var com = document.getElementById("Command");
	com.value = "Revert";
	document.submission.action="/Alpha/EditVersions";
	document.submission.submit();
}
function save()
{
	var descSurrogate = document.getElementById("descSurrogate");
	var description = document.getElementById("Description");
	var com = document.getElementById("Command");
	com.value = "Save";
	description.value = descSurrogate.value;
	document.submission.action="/Alpha/EditVersions";
    document.submission.submit();
}
function addVersion( group )
{
	var commandField = document.getElementById("Command");
	var groupField = document.getElementById("GroupId");
	commandField.value = "AddVersion";
	groupField.value = group;
	document.submission.action="/Alpha/EditVersions";
	document.submission.submit();
}
function addGroup( parent )
{
	var commandField = document.getElementById("Command");
	var parentField = document.getElementById("ParentId");
	commandField.value = "AddGroup";
	parentField.value = parent;
	document.submission.action="/Alpha/EditVersions";
	document.submission.submit();
}
function editGroup( group, id, parent )
{
	var groupField = document.getElementById("GroupName");
	var parentField = document.getElementById("ParentId");
	var groupIdField = document.getElementById("GroupId");
	parentField.value = parent;
	groupField.value = group;
	groupIdField.value = id;
	document.submission.action="/Alpha/EditVersions/EditGroup";
	document.submission.submit();
}
function editVersion( shortName, longName, id, groupId, backup )
{
	var shortNameField = document.getElementById("ShortName");
	var longNameField = document.getElementById("LongName");
	var versionIdField = document.getElementById("Version1");
	var backupField = document.getElementById("Backup");
	var groupIdField = document.getElementById("GroupId");
	versionIdField.value = id;
	groupIdField.value = groupId;
	shortNameField.value = shortName;
	longNameField.value = longName;
	backupField.value = backup;
	document.submission.action="/Alpha/EditVersions/EditVersion";
	document.submission.submit();
}
function redirect( action )
{
	var commandField = document.getElementById("Command");
	commandField.value = "";
	document.submission.action=action;
	document.submission.submit();
}
function notImplemented()
{
	alert("Sorry, this feature is not yet implemented");
}
function toggle( button, command )
{
	var groupId = button.id;
	var commandField = document.getElementById("Command");
	var groupField = document.getElementById("GroupId");
	groupField.value = groupId;
	commandField.value = command;
	document.submission.action="/Alpha/EditVersions";
	document.submission.submit();
}
// dynamically resize central column
function resize()
{
	var divCol = document.getElementById("centralColumn");
	var divHeader = document.getElementById("header");
	var height = window.innerHeight - divHeader.offsetHeight;
	divCol.style.height=height+"px";
}
</script>
</head>
<body onload="resize()">
<!-- header buttons -->
<div id="header" class="header">
<xsl:call-template name="writeTopButtons">
	<xsl:with-param name="writeSave">1</xsl:with-param>
	<xsl:with-param name="disabled">EditVersionsButton</xsl:with-param>
</xsl:call-template>
</div>

<!-- hidden fields -->
<div id="centralColumn" class="centralColumnBare">
<form name="submission" action="/Alpha/EditVersions" method="post">
<p class="versionHeader">
  <xsl:value-of select="//strings/string[@name='FilePrompt']/@value"/>
  <xsl:value-of select="//response/@file"/></p><p class="description">
  <xsl:value-of select="//strings/string[@name='DescriptionPrompt']/@value"/>
  <input type="text" id="descSurrogate" size="50"><xsl:attribute name="value">
   <xsl:value-of select="//response/@description"/></xsl:attribute>
  </input>
</p>

<!-- hidden submission fields -->
<input type="hidden" name="Version1" id="Version1" value="1"/>
<input type="hidden" name="GroupId" id="GroupId" value="1"/>
<input type="hidden" name="ParentId" id="ParentId" value="0"/>
<input type="hidden" name="File" id="File">
	<xsl:attribute name="value"><xsl:value-of select="$file"/></xsl:attribute>
</input>
<input type="hidden" name="Command" id="Command" value=""/>
<input type="hidden" name="Description" id="Description"/>
<input type="hidden" name="GroupName" id="GroupName" value=""/>
<input type="hidden" name="ShortName" id="ShortName" value=""/>
<input type="hidden" name="LongName" id="LongName" value=""/>
<input type="hidden" name="Backup" id="Backup" value="0"/>

<xsl:for-each select="/response/groups/group">
	<ul>
	<xsl:call-template name="printGroup">
		<xsl:with-param name="addGroupName">
			<xsl:value-of select="$addGroupName"/>
		</xsl:with-param>
		<xsl:with-param name="addVersionName">
			<xsl:value-of select="$addVersionName"/>
		</xsl:with-param>
		<xsl:with-param name="editGroupName">
			<xsl:value-of select="$editGroupName"/>
		</xsl:with-param>
		<xsl:with-param name="editVersionName">
			<xsl:value-of select="$editVersionName"/>
		</xsl:with-param>
		<xsl:with-param name="deleteButtonName">
			<xsl:value-of select="$deleteButtonName"/>
		</xsl:with-param>
	</xsl:call-template>
	</ul>
</xsl:for-each>
<!-- add one insert group button at the end -->
<ul>
<xsl:call-template name="printAddGroupButton">
	<xsl:with-param name="addGroupName">
		<xsl:value-of select="$addGroupName"/>
	</xsl:with-param>
	<xsl:with-param name="parent" select="0"/>
</xsl:call-template>
</ul>
</form>
</div>
</body>
</html>
</xsl:template>

<!-- Print a group together with its subgroups and versions -->
<xsl:template name="printGroup">
<xsl:param name="addGroupName"/>
<xsl:param name="addVersionName"/>
<xsl:param name="editGroupName"/>
<xsl:param name="editVersionName"/>
<xsl:param name="deleteButtonName"/>
<xsl:variable name ="backup" select="@backup"/>
<xsl:if test="$backup=''">
	<xsl:variable name="backup" select="0"/>
</xsl:if>
<!-- expand or collapse button -->
<li class="group">
<xsl:choose>
    <xsl:when test="count(./group | ./version)>0">
        <input type="button" onclick="toggle(this,'Collapse')">
    	    <xsl:attribute name="id"><xsl:value-of select="@id"/></xsl:attribute>
    	     <xsl:attribute name="value">-</xsl:attribute>
    	 </input>
   	</xsl:when>
	<xsl:when test="count(./group | ./version)=0">
	    <input type="button" onclick="toggle(this,'Expand')">
			<xsl:attribute name="id"><xsl:value-of select="@id"/></xsl:attribute>
			<xsl:attribute name="value">+</xsl:attribute>
		</input>
	</xsl:when>
</xsl:choose>
<xsl:text> </xsl:text><xsl:value-of select="@name"/>
<!-- add a delete button -->
<xsl:text> </xsl:text>
<a>
	<xsl:attribute name="href">
		<xsl:value-of select="concat('javascript:','deleteGroup(',@id,')')"/>
	</xsl:attribute>
	<!-- text of the edit group button -->
	<xsl:value-of select="$deleteButtonName"/>
</a>
<!-- add an edit group button -->
<xsl:text>, </xsl:text>
<a>
	<xsl:attribute name="href">
		<xsl:value-of select="concat('javascript:','editGroup(','&quot;',@name,'&quot;',',',@id,',',@parent,')')"/>
	</xsl:attribute>
	<!-- text of the edit group button -->
	<xsl:value-of select="$editGroupName"/>
</a>
</li>
<xsl:if test="count(./group | ./version)>0">
<ul>
<!-- print sub-groups -->
<xsl:for-each select="./group">
	<xsl:call-template name="printGroup">
		<xsl:with-param name="addGroupName">
			<xsl:value-of select="$addGroupName"/>
		</xsl:with-param>
		<xsl:with-param name="editGroupName">
			<xsl:value-of select="$editGroupName"/>
		</xsl:with-param>
		<xsl:with-param name="editVersionName">
			<xsl:value-of select="$editVersionName"/>
		</xsl:with-param>
		<xsl:with-param name="addVersionName">
			<xsl:value-of select="$addVersionName"/>
		</xsl:with-param>
		<xsl:with-param name="deleteButtonName">
			<xsl:value-of select="$deleteButtonName"/>
		</xsl:with-param>
	</xsl:call-template>
</xsl:for-each>
<!-- add group button definition at the end -->
<li class="group"><xsl:call-template name="printAddGroupButton">
	<xsl:with-param name="addGroupName">
		<xsl:value-of select="$addGroupName"/>
	</xsl:with-param>
	<xsl:with-param name="parent" select="@id"/>
</xsl:call-template>
</li>
<!-- print version definitions -->
<xsl:for-each select="./version">
	<li class="version">Version: <xsl:value-of select="@shortName"/>
	<xsl:text> </xsl:text>(<xsl:value-of select="@longName"/>)
	<xsl:if test="@backup>0"><em> partial</em></xsl:if>
	<!-- add a delete button -->
	<xsl:text> </xsl:text>
	<a>
		<xsl:attribute name="href">
			<xsl:value-of select="concat('javascript:','deleteVersion(',@id,')')"/>
		</xsl:attribute>
		<!-- text of the edit group button -->
		<xsl:value-of select="$deleteButtonName"/>
	</a>
	<!-- add an edit version button -->
	<xsl:text>, </xsl:text>
	<a>
		<xsl:attribute name="href">
			<xsl:value-of select="concat('javascript:','editVersion(','&quot;',@shortName,'&quot;',',','&quot;',@longName,'&quot;',',',@id,',',@group,',',@backup,')')"/>
		</xsl:attribute>
		<!-- text of the edit version button -->
		<xsl:value-of select="$editVersionName"/>
	</a>
	</li>
</xsl:for-each>
</ul>
</xsl:if>
<ul>
<!-- add one version button definition after all the versions -->
<!-- or if no versions present in group - allow the user to add one -->
<li class="version">
<xsl:call-template name="printAddVersionButton">
	<xsl:with-param name="addVersionName">
		<xsl:value-of select="$addVersionName"/>
	</xsl:with-param>
	<xsl:with-param name="group" select="@id"/>
</xsl:call-template>
</li>
</ul>
</xsl:template>

<!-- button to add a group at the end of a list of groups -->
<xsl:template name="printAddGroupButton">
<xsl:param name="addGroupName"/>
<xsl:param name="parent"/>
<a>
	<xsl:variable name="script" select="concat('javascript:','addGroup(',$parent,')')"/>
	<xsl:attribute name="href">
		<xsl:value-of select="$script"/>
	</xsl:attribute>
	<!-- text of the add group button -->
	<xsl:value-of select="$addGroupName"/>
</a>

</xsl:template>

<!-- button to add a version at the end of a list of versions -->
<xsl:template name="printAddVersionButton">
<xsl:param name="addVersionName"/>
<xsl:param name="group"/>
<a>
	<xsl:attribute name="href">
		<xsl:value-of select="concat('javascript:','addVersion(',$group,')')"/>
	</xsl:attribute>
	<xsl:value-of select="$addVersionName"/>
</a>
</xsl:template>

</xsl:stylesheet>